package com.kanshu.ksgb.fastread.doudou.app;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.LifecycleHelper;
import com.kanshu.ksgb.fastread.doudou.common.business.routerservice.ConfigService;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.UserUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ActivityMgr;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.kanshu.ksgb.fastread.doudou.common.versioncheck.VersionUpdateManager;
import com.ss.android.common.applog.TeaAgent;

/**
 * Created by wcy on 2017\6\16 0016
 */
public class LifecycleHandler implements Application.ActivityLifecycleCallbacks {
    private LifecycleHelper mLifecycleHelper;

    public LifecycleHandler() {
    }

    public LifecycleHandler(LifecycleHelper lifecycleHelper) {
        mLifecycleHelper = lifecycleHelper;
    }


    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        // 把actvity放到application栈中管理
        ActivityMgr.getInstance().addActivity(activity);
        if (mLifecycleHelper != null) {
            mLifecycleHelper.onActivityCreated(activity);
        }
    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (mLifecycleHelper != null) {
            mLifecycleHelper.onActivityStarted(activity);
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
        String tag = ActivityMgr.getInstance().getActivityTag(activity);
        boolean flg = TextUtils.equals(tag, ARouterConfig.PageTagParams.SPLASH_PAGE);
        if (TextUtils.isEmpty(UserUtils.getUserId()) && !flg) {
            // 根据设备注册id
            ARouter.getInstance().navigation(ConfigService.class).registerIdByDevice();
        }

        if (mLifecycleHelper != null) {
            mLifecycleHelper.onActivityResumed(activity);
        }
        //头条时长统计
        TeaAgent.onResume(activity);
    }

    @Override
    public void onActivityPaused(Activity activity) {
        if (mLifecycleHelper != null) {
            mLifecycleHelper.onActivityPaused(activity);
        }
        TeaAgent.onPause(activity);
    }

    @Override
    public void onActivityStopped(Activity activity) {
        if (mLifecycleHelper != null) {
            mLifecycleHelper.onActivityStopped(activity);
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        if (mLifecycleHelper != null) {
            mLifecycleHelper.onActivitySaveInstanceState(activity);
        }
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        VersionUpdateManager.getInstance().clearReference();
        if (mLifecycleHelper != null) {
            mLifecycleHelper.onActivityDestroyed(activity);
        }
        ActivityMgr.getInstance().removeActivity(activity);
        //ToastUtil.cancelStaticToast();
    }
}
